<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18
 * Time: 15:40
 */

namespace addons\seller\controller;

use app\common\Code;
use app\common\model\CommunitySellerRelation;
use app\common\Session;
class Community extends Common
{
    /**
     * 商家的社区列表
     */
    public function seller_community_list()
    {
        $this->check_login();
        $seller = Session::get_local_seller();
        if (empty($seller)) {
            response_json(Code::NO_LOGIN);
        }
        $param = input();
        $this->assign('param',$param);
        $relation = \addons\seller\service\CommunitySellerRelation::get_seller_community_by_seller_id($seller->id,$param);
        $this->assign("data",$relation);
        return $this->fetch();
    }

    /**
     * 商家新增/编辑社区视图
     */
    public function seller_community_view()
    {
        $this->check_login();
        $seller = Session::get_local_seller();
        if (empty($seller)) {
            response_json(Code::NO_LOGIN);
        }
        $community_model = new \app\common\model\Community();
        $community = $community_model->get_all_community();  #获取全部社区
        $relation_model = new \addons\seller\service\CommunitySellerRelation();
        $selected_community_ids = $relation_model->get_community_column_by_seller_id($seller->id);
        foreach($community as $k=>$v)
        {
            if(in_array($v['id'],$selected_community_ids))
            {
                 $community[$k]['select_'] = 1;   #商家存在此社区
            }else
            {
                $community[$k]['select_'] = 0;    #商家不存在此社区
            }
        }
        $this->assign("community",$community);
        return $this->fetch();
    }

    /**
     *商家-社区管理
     */
    public function seller_community_edit()
    {
        $this->check_login();
        $seller = Session::get_local_seller();
        if (empty($seller)) {
            response_json(Code::NO_LOGIN);
        }
        $param = input();
        $relation_model = new CommunitySellerRelation();
        $relation_model->delSellerCommunityRelation($seller->id);
        if(isset($param['community'])){
            $relation_model->addListSellerCommunityRelation($seller->id,$param['community']);
        }
        response_json(Code::NONE,Code::msg(Code::NONE));
    }




}